﻿using System;
using System.Collections.Generic;
using System.Linq;
using System.Net;
using System.Net.Http;
using System.Web.Http;
using System.Text;
using System.Net.Mail;

using AttributeRouting;
using AttributeRouting.Web.Mvc;
using Newtonsoft.Json.Linq;
using Phatent.Model.Results;
using Phatent.Model.Pages;
using Phatent.Logic.Pages;
using Phatent.Model.Functions;
using Phatent.Logic.Functions;

namespace Phatent.Website.Areas.Admin.Controllers
{
    [RouteArea("Admin")]
    [RoutePrefix("Fun")]
    [Authorize]
    public class FunApiController : ApiController
    {
        [Route("GetMessages")]
        [HttpPost]
        public JsonEntitySet<MessageModel> GetMessages(JObject request)
        {
            string errinfo = "no data";
            try
            {
                MessageQuery sea = new MessageQuery();
                sea.PageIndex = request.Value<int>("pageIndex");
                sea.PageSize = request.Value<int>("pageSize");
                var result = MessageLogic.Gets(sea);
                return new JsonEntitySet<MessageModel>(result);
            }
            catch (Exception ex)
            {
                errinfo = ex.Message;
            }
            return new JsonEntitySet<MessageModel>(null, errinfo, false);
        }

        [Route("DeleteMessage")]
        [HttpPost]
        public JsonResult<bool> DeleteMessage(JObject request)
        {
            string errinfo = "对不起！删除失败，出错原因：";
            try
            {
                #region 收集参数
                var id = request.Value<int>("id");
                #endregion
                MessageLogic.Delete(id);
                return new JsonResult<bool>(true);
            }
            catch (Exception ex)
            {
                errinfo = ex.Message;
            }
            return new JsonResult<bool>(false, errinfo, false);
        }

        [Route("GetMeetings")]
        [HttpPost]
        public JsonEntitySet<MeetingModel> GetMeetings(JObject request)
        {
            string errinfo = "no data";
            try
            {
                MessageQuery sea = new MessageQuery();
                sea.PageIndex = request.Value<int>("pageIndex");
                sea.PageSize = request.Value<int>("pageSize");
                var result = MeetingLogic.Gets();
                return new JsonEntitySet<MeetingModel>(result);
            }
            catch (Exception ex)
            {
                errinfo = ex.Message;
            }
            return new JsonEntitySet<MeetingModel>(null, errinfo, false);
        }

        [Route("SaveMeeting")]
        [HttpPost]
        public JsonResult<bool> SaveMeeting(JObject request)
        {
            string errinfo = "对不起！保存失败，出错原因：";
            try
            {
                #region 收集参数
                var model = new MeetingModel();
                model.ID = request.Value<int>("id");
                model.Title = request.Value<string>("title");
                model.AdminPassword = request.Value<string>("adminPassword");
                model.JoinPassword = request.Value<string>("joinPassword");
                #endregion

                MeetingLogic.Update(model);
                return new JsonResult<bool>(true);
            }
            catch (Exception ex)
            {
                errinfo = ex.Message;
            }
            return new JsonResult<bool>(false, errinfo, false);
        }

        [Route("SendEmployee")]
        [HttpPost]
        public JsonResult<bool> SendEmployee(JObject request)
        {
            string errinfo = "对不起！保存失败，出错原因：";
            try
            {
                var guid = request.Value<string>("guid");
                if (!string.IsNullOrEmpty(guid))
                {
                    var model = EmployeeLogic.GetModel(new Guid(guid));
                    if (model != null)
                    {
                        var page = PageLogic.GetPage(43, model.EmployeeName);
                        if (page != null)
                        {
                            var url = string.Format("http://{0}/Home/Function/ShowEmployee/{1}", System.Web.HttpContext.Current.Request.Url.Host, guid);

                            MailMessage message = new MailMessage();
                            message.Body = string.Format("非常感谢你的关注，请点击以下链接，了解我们的职员信息<br /><a href='{0}'>{0}</a>", url);
                            message.Subject = "大知科技股份有限公司--任职查询地址";
                            message.IsBodyHtml = true;
                            message.HeadersEncoding = ASCIIEncoding.UTF8;
                            message.BodyEncoding = ASCIIEncoding.UTF8;
                            message.SubjectEncoding = ASCIIEncoding.UTF8;
                            //message.From = new MailAddress("zgq@phatent.cn");
                            message.To.Add(new MailAddress(model.Email));
                            SmtpClient client = new SmtpClient();
                            client.Send(message);
                        }
                    }
                }
                return new JsonResult<bool>(true);
            }
            catch (Exception ex)
            {
                errinfo = ex.Message;
            }
            return new JsonResult<bool>(false, errinfo, false);
        }

        [Route("GetEmployees")]
        [HttpPost]
        public JsonEntitySet<EmployeeModel> GetEmployees(JObject request)
        {
            string errinfo = "no data";
            try
            {
                MessageQuery sea = new MessageQuery();
                sea.PageIndex = request.Value<int>("pageIndex");
                sea.PageSize = request.Value<int>("pageSize");
                var result = EmployeeLogic.Gets(sea);
                return new JsonEntitySet<EmployeeModel>(result);
            }
            catch (Exception ex)
            {
                errinfo = ex.Message;
            }
            return new JsonEntitySet<EmployeeModel>(null, errinfo, false);
        }
    }
}
